﻿using System;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;

namespace LMS.SMSH.WEB
{
    public partial class AdminLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
        {

            #region 假如浏览器禁用Cookies，则提示用户开启Cookies
            if (Request.Cookies["ValidateCode"] == null)
            {
                string strMessage = string.Format("alert('您的浏览器设置已禁用Cookies，您必须启用浏览器的Cookies选项后才能使用本系统!')");
                this.ClientScript.RegisterStartupScript(this.GetType(), "", strMessage, true);
                return;
            }
            #endregion

            #region 比较验证码，不相同就提示
            if (string.Compare(Request.Cookies["ValidateCode"].Value, txtValidate.Value.Trim()) == 0)
            {
                #region 将界面值传入实体对象
                TB_admin admin = new TB_admin();
                admin.mesAdminId = txtAdminID.Value.Trim();
                admin.adminPwd = txtPwd.Value.Trim();
                #endregion

                #region 调用存储过程，判断用户是否为管理员
                int? result = -1;
                LMSDataContext.GetLMSDB.sp_Isadmin(admin.mesAdminId, admin.adminPwd, ref result);
                switch (result)
                {
                    case 1:
                        //表示成功登陆
                        Session["adminID"] = admin.mesAdminId;
                        //string strMessage = string.Format("alert('" + Session["adminID"].ToString() + "');location.href='Admindex.html'");
                       // this.ClientScript.RegisterStartupScript(this.GetType(), "", strMessage , true);
                        //this.ClientScript.RegisterStartupScript(this.GetType(), "", "window.location.href='Admindex.html'", true);
                        Response.Redirect("Admindex.html");
                        break;
                    case 0:
                        //密码错误（用户名正确）
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('密码输入错误！');", true);
                        break;
                    case -1:
                        //用户名不存在
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('用户ID不存在！');", true);
                        break;
                    default:
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('数据执行错误！');", true);
                        break;
                }

                #endregion
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('验证码错误！');", true);
            }
            #endregion
        }


    }
}